<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增物料</title>
<link href="static/css/style.css" rel="stylesheet" type="text/css" />
<style>
li>span{  
    white-space: nowrap;  /*强制span不换行*/
    display: inline-block;  /*将span当做块级元素对待*/
    margin-left:10px;
}
.error{
    color:red;
}
</style>
</head>

<body>

	<div class="place">
    <span>位置：</span>
    <ul class="placeul">
    <li><a href="user/right">首页</a></li>
    </ul>
    </div>
    
    <div class="formbody">
    
    
    <div id="usual1" class="usual"> 
    
    <div class="itab">
  	<ul> 
    <li><a href="#tab1" class="selected">新增物料</a></li> 
  	</ul>
    </div> 
    
    <form id="addMaterialForm" action="" method="post">
  	<div id="tab1" class="tabson">
    
    <div class="formtext">Hi，<b>${loginUser.username}</b>，欢迎您试用新增物料功能！</div>
    <input type="hidden" id="purchaseId" name="purchaseId"  value="" />
    <ul class="forminfo">
    <li><label>名称<b>*</b></label><input id="name" name="name" type="text" class="dfinput" value=""  style="width:250px;"/></li>
   	<li><label>单价<b>*</b></label><input id="unitPrice" name="unitPrice" type="text" class="dfinput" value=""  style="width:250px;" /></li>
 	<li><label>数量<b>*</b></label><input id="amount" name="amount" type="text" class="dfinput" value=""  style="width:250px;" /></li>
 	<li><label>总价<b>*</b></label><input id="totalPrice" name="totalPrice" type="text" class="dfinput" value=""  style="width:250px;"/></li>  	 	
    <li><label>&nbsp;</label><input id="submitBtn" type="submit" class="btn" value="提交"/></li>
    </ul>
    
    </div> 
    </form>
    
	</div> 
        
    
    </div>
<script language="javascript" src="static/js/jquery.min.js"></script>
<script type="text/javascript" src="static/js/jquery.idTabs.min.js"></script>
<script src="static/layer/layer.js" charset="utf-8"></script>
<!-- 校验 -->
<script language="javascript" src="static/js/jquery.validate.min.js"></script>
<script language="javascript" src="static/js/messages_zh.js"></script>

<script type="text/javascript">
$("#usual1 ul").idTabs();
$('.tablelist tbody tr:odd').addClass('odd');
</script>
<script type="text/javascript">

$('#purchaseId').val(parent.$('#purchaseId').val()); // 获取从父页面传来的订单编号

/**
 * 计算总价
 */
function countTotalPrice(){
    var unitPrice = $('#unitPrice').val();
    var amount = $('#amount').val();
    $('#totalPrice').val(unitPrice * amount);
}

$("#unitPrice").bind('input porpertychange',function(){
	countTotalPrice();
});
$("#amount").bind('input porpertychange',function(){
	countTotalPrice();
});

  

  function add(){
	  var material = $("#addMaterialForm").serialize();
	  console.log(material);
	  
	  $.post('material/add',material,function(result){
		  if(null != result.code && result.code >= 1){
	          //获取窗口索引
	          var index = parent.layer.getFrameIndex(window.name);
	          //关闭iframe页面
	          //parent.layer.close(index);
		      parent.layer.alert('操作成功',function(){		    	  
		    	  parent.layer.close(index);
		    	  parent.location.reload(); // 刷新父窗口
		      });
		     
		      return;
		  }
		  parent.layer.alert('操作失败,请稍后再试');
	  },'json');
  }
  
</script>

<script type="text/javascript">
/** validate */
 $.validator.setDefaults({
    submitHandler: function() {
    	add();
    }
});

  $().ready(function(){
    var validate = $("#addMaterialForm").validate({
        errorPlacement: function(error, element) {
        	error.insertAfter(element); 
        },
        errorElement: "span",
        rules:{
        	name:{
                required:true
            },
            unitPrice:{
                required:true,
                number:true
            },
            amount:{
                required:true,
                number:true
            },
            totalPrice:{
                required:true,
                number:true
            }
            },
        messages:{
        	name:{
                required:"请输入名称"
            },
            unitPrice:{
            	required:"请输入单价",
            	number:"必须输入合法的数字"
            },
            amount:{
            	required:"请输入数量",
            	number:"必须输入合法的数字"
            },
            totalPrice:{
                required:"请输入总价",
                number:"必须输入合法的数字"
            }                                    
        }
                  
    });    

});
</script>

</body>

</html>
